package development.group;

import state.GameForm;
import state.GameGlobal;
import state.Graphics;
import state.GroupForm;
import state.Key;
import state.component.BottomBar;
import state.component.Event;
import state.component.EventListener;
import state.component.MultiTab;
import state.util.Hashtable;
import development.Constants;
import development.DrawUtils;
import development.FrmDownloadResource;
import development.FrmFriend;
import development.FrmListFreeUser;
import development.FrmTable;
import development.Global;

public class FrmGroupTableFreeUserFriend extends GroupForm implements EventListener {
  private MultiTab multiTab;
  private static GameForm singleton;
  public static GameForm backForm;
  
  public static GameForm instance() {
    if (singleton == null) {
      singleton = new FrmGroupTableFreeUserFriend();
    }
    return singleton;
  }
  
  public void init(Hashtable parameters) {
    multiTab = new MultiTab(this, Constants.SCREEN_WIDTH, Constants.SCREEN_HEIGHT);
    multiTab.addTab("Bàn chơi", FrmTable.instance(), parameters);
    multiTab.addTab("Người chơi", FrmListFreeUser.instance(), parameters);
    multiTab.addTab("Bạn bè", FrmFriend.instance(), parameters);
    multiTab.setStyle(Constants.MULTI_TAB_STYLE);
    multiTab.selectTab(0);
  }
  
  public static void setTab(int index) {
    FrmGroupTableFreeUserFriend frmFriendAndTop = (FrmGroupTableFreeUserFriend) FrmGroupTableFreeUserFriend.instance();
    MultiTab multiTab = frmFriendAndTop.getMultiTab();
    if (multiTab != null) {
      multiTab.selectTab(index);
    }
  }
  
  public MultiTab getMultiTab() {
    return multiTab;
  }
  
  public void draw(Graphics g) {
    DrawUtils.drawBackground(g);
  }
  
  public void goBack() {
    if (backForm != null) {
      Global.nextState(backForm, null);
      backForm = null;
    } else {
      if (Global.systemCanvas.lastState instanceof FrmDownloadResource) {
        GameGlobal.nextState(FrmGroupMenu.instance(), null);
      } else {
        GameGlobal.nextState(Global.systemCanvas.lastState, null);
      }
    }
  }
  
  public void keyReleased(int keyCode) {
    switch (keyCode) {
    case Key.SOFT_LEFT:
      if (multiTab.getCurrentSelectedTab() == 0) {
        FrmTable frmTable = (FrmTable) FrmTable.instance();
        frmTable.checkToAddSubMenu();
      }
      break;
    case Key.LEFT:
      multiTab.previousTab();
      break;
    case Key.RIGHT:
      boolean isNextTab = true;
      if (multiTab.getCurrentSelectedTab() == 0) {
        BottomBar bottomBar = DrawUtils.getBottomBar(_manager);
        if (bottomBar != null) {
          if (((bottomBar.getLeftMenu() != null) && bottomBar.getLeftMenu().isShowing()) || ((bottomBar.getRightMenu() != null) && bottomBar.getRightMenu().isShowing())) {
            isNextTab = false;
          }
        }
      }
      
      if (isNextTab) {
        multiTab.nextTab();
      }
      break;
    }
  }
  
  public void destroy() {
    multiTab.detroy();
    multiTab = null;
    FrmTable.instance().detroyAll();
    FrmListFreeUser.instance().detroyAll();
    FrmFriend.instance().detroyAll();
  }

  public void onActionPerform(Event event) {
    if (multiTab.getCurrentSelectedTab() == 0) {
      FrmTable frmTable = (FrmTable) FrmTable.instance();
      frmTable.onActionPerform(event);
    }
  }
}
